package cn.e3.search.service.impl;

import java.util.List;









import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import search.SearchItem;
import cn.e3.search.mapper.ItemMapper;
import cn.e3.search.service.SearchItemService;
import cn.e3.utils.E3Result;
@Service
public class SearchItemServiceImpl implements SearchItemService{
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private SolrServer solrServer;
	
	public E3Result selectItem() {
		try {
			//查询商品获得内容
			List<SearchItem> list = itemMapper.selectItem();
			//遍历商品，将商品加入服务器
			for (SearchItem searchItem : list) {
				//创建文档对象
				SolrInputDocument document=new SolrInputDocument();
				//将商品信息房屋域中
				document.addField("id", searchItem.getId());
				document.addField("item_title", searchItem.getTitle());
				document.addField("item_sell_point", searchItem.getSell_point());
				document.addField("item_price", searchItem.getPrice());
				document.addField("item_image", searchItem.getImage());
				document.addField("item_category_name", searchItem.getCategory_name());
				//将文档对象添加到服务器中
				solrServer.add(document);
			}
			solrServer.commit();
			return E3Result.ok();
		
		} catch (Exception e) {
			e.printStackTrace();
			return E3Result.build(500, "数据导入异常");
		}	
	}
}
